﻿using System;
using System.Collections.Generic;
using System.Web;
using Jxym.wlkc2011.Module.Default.model;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.CMS.MODEL;


namespace Jxym.wlkc2011
{
    /// <summary>
    /// 常用信息获取类
    /// </summary>
    public class MyGet : System.Web.UI.Page
    {
        public MyGet() { }

        public static string KCXX(object var)
        {
            if (var.ToString().Contains("课程学习"))
            {
                return "x";
            }
            else if (var.ToString().Contains("相关资源"))
            {
                return "p";
            }
            else
            {
                return "m";
            }
        }

        public static void GetIDByName(Jxym.wlkc2011.WebSite.zjgx var, string menuName)
        {
            var.Visible = true;
            var.MyTitle = menuName;
        }

        public static string LoadFlashView(string siteName, string classTitle)
        {
            if (siteName == "") { return ""; }

            string _where;

            if (classTitle == "")
            {
                _where = String.Format("ViewPhoto<>'{0}' AND ViewPhoto<>''", "no.jpg");
            }
            else
            {
                MsSqlDal<Cms_ContentClass> ccc = new MsSqlDal<Cms_ContentClass>();
                Cms_ContentClass cccInfo = ccc.GetModel(String.Format("siteName='{0}' AND Title='{1}'", siteName, classTitle), null);
                _where = String.Format("ViewPhoto<>'{0}' AND ViewPhoto<>'' AND ContentClassID={1}", "no.jpg", cccInfo.ID);
            }

            string scriptStr = "";

            MsSqlDal<Cms_Content> cc = new MsSqlDal<Cms_Content>();

            IList<Cms_Content> list = cc.GetList(_where, null, "AddTime DESC", 6);

            foreach (Cms_Content ccInfo in list)
            {
                scriptStr += "<a href=\"" + siteName + "_m" + ccInfo.ContentClassID + "_c" + ccInfo.ID + ".xhtml\" title=\"" + ccInfo.Title + "\"><img alt=\"" + ccInfo.Title + "\" src=\"UpFiles/" + siteName + "/" + ccInfo.ViewPhoto + "\" /></a>";
            }

            return scriptStr;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static User_Info GetUserInfoByID(string userId)
        {
            MsSqlDal<User_Info> iUser = new MsSqlDal<User_Info>();
            User_Info info = iUser.GetModel("ID=" + userId, null);
            if (info != null)
                return info;
            return new User_Info();
        }

        /// <summary>
        /// 根据部门ID得到用户部门名称
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static Depmet GetDeptInfoByID(int depId)
        {
            MsSqlDal<Depmet> iUser = new MsSqlDal<Depmet>();
            Depmet info = iUser.GetModel("ID=" + depId, null);
            if (info != null)
                return info;
            return new Depmet();
        }

        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="command">命令 [可用命令 S|A|D|U ]</param>
        /// <param name="modelId">模块编号</param>
        /// <returns></returns>
        public static bool GetPermissionsValidation(int modelId, string command, string userId)
        {
            try
            {
                MsSqlDal<User_Info> iUi = new MsSqlDal<User_Info>();

                bool validation = false;

                MsSqlDal<User_RoleConfig> iUrc = new MsSqlDal<User_RoleConfig>();

                IList<User_RoleConfig> urcList = iUrc.GetList("UserID=" + Convert.ToInt32(userId), null, "", 0);

                string roles = "";

                foreach (User_RoleConfig urc in urcList)
                {
                    roles += " RoleID=" + urc.RoleID + " or";
                }

                roles = roles.TrimEnd("or".ToCharArray());

                MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();

                User_RolePermissionConfig urpc = iUrpc.GetModel("(" + roles + ") AND MenuID=" + modelId, null);

                if (urpc == null) return false;

                if (urpc.PermissionValue != "" && urpc.PermissionValue.Length == 4)
                {
                    char[] pvs = urpc.PermissionValue.ToCharArray();

                    switch (command)
                    {
                        default://查
                            if (pvs[0] == '1') validation = true;
                            break;
                        case "A"://增
                            if (pvs[1] == '1') validation = true;
                            break;
                        case "U"://改
                            if (pvs[2] == '1') validation = true;
                            break;
                        case "D"://删
                            if (pvs[3] == '1') validation = true;
                            break;
                    }
                }
                return validation;
            }
            catch
            {
                return false;
            }
        }
    }
}